#include "tieta_zk_v1_3.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tmodbus/tmodbus.h"

#define hardware_version                              "1000"  // 硬件版本
#define soft_version                                  "1001"  // 软件版本
#define bms_manufacturer                              "QTHN"  // BMS厂家
#define rated_voltage                                 60      // 额定电压，V
#define rated_capacity                                42      // 额定容量，AH
#define series_number_of_cells                        16      // 串联数量
#define number_of_cells_in_parallel                   7       // 并联数量
#define bms_communication_protocol_soft_version_big   1       // 通讯协议大版本号
#define bms_communication_protocol_soft_version_small 3       // 通讯协议小版本号

#define register_table_len    86
#define modbus_dev_table_size 1
#define sendbuffer_length     256

#define WORD_16_L_BYTE 0
#define WORD_16_H_BYTE 1
typedef struct {
  uint8_t _0 : 1;
  uint8_t _1 : 1;
  uint8_t _2 : 1;
  uint8_t _3 : 1;
  uint8_t _4 : 1;
  uint8_t _5 : 1;
  uint8_t _6 : 1;
  uint8_t _7 : 1;
} _8bit;

typedef union {
  uint8_t data;
  _8bit   bit;
} uint8_bit;

typedef union {
  uint8_bit byte[8];
  uint16_t  word_16;
  uint32_t  word_32;
  uint64_t  word_64;
} typ_iow;

typedef enum {
  dev_bms_addr = 0X01,  // bms
  dev_cs1_addr,         // 测试预留1
  dev_cs2_addr,         // 测试预留2
  dev_mc_addr,          // MC，电机控制器
  dev_ecu_addr,         // ECU,智能中控(主机)
  dev_tks_addr,         // 头盔锁
  dev_hls_addr,         // 后轮锁
  dev_yb_addr,          // 仪表
  dev_zts_addr,         // 座桶锁
} dev_addr_t;

static uint8_t sendbuffer[sendbuffer_length];

static bool test_switch_flag = 1;
static char sn_code[24]      = {"BT104802004TTNY231206001"};

// 电池实时状态(Battery real-time status)
static uint16_t total_battery_pack_pressure_s  = 0;
static uint16_t number_of_cells_s              = 0;
static uint16_t soc_s                          = 0;
static uint16_t residual_capacity_s            = 0;
static uint16_t soh_s                          = 0;
static uint16_t charge_and_discharge_current_s = 0;
static uint16_t ambient_temp_s                 = 0;
static uint16_t cell_min_temp_s                = 0;
static uint16_t mos_temp_s                     = 0;
static uint16_t cell_voltage_1_s               = 0;
static uint16_t cell_voltage_2_s               = 0;
static uint16_t cell_voltage_3_s               = 0;
static uint16_t cell_voltage_4_s               = 0;
static uint16_t cell_voltage_5_s               = 0;
static uint16_t cell_voltage_6_s               = 0;
static uint16_t cell_voltage_7_s               = 0;
static uint16_t cell_voltage_8_s               = 0;
static uint16_t cell_voltage_9_s               = 0;
static uint16_t cell_voltage_10_s              = 0;
static uint16_t cell_voltage_11_s              = 0;
static uint16_t cell_voltage_12_s              = 0;
static uint16_t cell_voltage_13_s              = 0;
static uint16_t cell_voltage_14_s              = 0;
static uint16_t cell_voltage_15_s              = 0;
static uint16_t cell_voltage_16_s              = 0;
static uint16_t cell_voltage_17_s              = 0;
static uint16_t cell_voltage_18_s              = 0;
static uint16_t cell_voltage_19_s              = 0;
static uint16_t cell_voltage_20_s              = 0;
static uint16_t cell_max_temp_s                = 0;
static uint16_t number_of_battery_cycles_s     = 0;
static uint16_t battery_discharge_power_max_s  = 0;
static uint16_t year_s                         = 0;
static uint16_t month_and_day_s                = 0;
static uint16_t hour_and_minute_s              = 0;
static uint16_t second_s                       = 0;

// 电池概要信息(Battery profile)
static uint16_t bms_hardware_version_1_s                  = 0;
static uint16_t bms_hardware_version_2_s                  = 0;
static uint16_t bms_hardware_version_3_s                  = 0;
static uint16_t bms_hardware_version_4_s                  = 0;
static uint16_t bms_hardware_version_5_s                  = 0;
static uint16_t bms_hardware_version_6_s                  = 0;
static uint16_t bms_hardware_version_7_s                  = 0;
static uint16_t bms_hardware_version_8_s                  = 0;
static uint16_t bms_hardware_version_9_s                  = 0;
static uint16_t bms_hardware_version_10_s                 = 0;
static uint16_t bms_soft_version_1_s                      = 0;
static uint16_t bms_soft_version_2_s                      = 0;
static uint16_t bms_soft_version_3_s                      = 0;
static uint16_t bms_soft_version_4_s                      = 0;
static uint16_t bms_soft_version_5_s                      = 0;
static uint16_t bms_manufacturer_1_s                      = 0;
static uint16_t bms_manufacturer_2_s                      = 0;
static uint16_t bms_battery_voltage_and_capacity_s        = 0;
static uint16_t bms_series_parallel_quantity_s            = 0;
static uint16_t bms_communication_protocol_soft_version_s = 0;
static uint16_t reserve_s                                 = 0;

static uint16_t sn_code_1_s  = 0;
static uint16_t sn_code_2_s  = 0;
static uint16_t sn_code_3_s  = 0;
static uint16_t sn_code_4_s  = 0;
static uint16_t sn_code_5_s  = 0;
static uint16_t sn_code_6_s  = 0;
static uint16_t sn_code_7_s  = 0;
static uint16_t sn_code_8_s  = 0;
static uint16_t sn_code_9_s  = 0;
static uint16_t sn_code_10_s = 0;
static uint16_t sn_code_11_s = 0;
static uint16_t sn_code_12_s = 0;
static uint16_t sn_code_13_s = 0;
static uint16_t sn_code_14_s = 0;

// 开关量寄存器表(非标，如果是标准的开关量不可能会和寄存器混合一起)
static uint16_t switch_B001_s = 0;
static uint16_t switch_B002_s = 0;
static uint16_t switch_B003_s = 0;
static uint16_t switch_B004_s = 0;
static uint16_t switch_B005_s = 0;
static uint16_t switch_B006_s = 0;

static modbus_register_table_value_t register_table[register_table_len] = {
    // 电池实时状态(Battery real-time status)
    {"total_battery_pack_pressure", 0x0000, &total_battery_pack_pressure_s, false},   /* 电池包实际总电压 0.01V */
    {"number_of_cells", 0x0001, &number_of_cells_s, false},                           /* 电芯数量 */
    {"soc", 0x0002, &soc_s, false},                                                   /* SOC 1% */
    {"residual_capacity", 0x0003, &residual_capacity_s, false},                       /* 剩余容量 0.01Ah */
    {"soh", 0x0004, &soh_s, false},                                                   /* SOH 1% */
    {"charge_and_discharge_current", 0x0005, &charge_and_discharge_current_s, false}, /* 电池包实际充放电电流 0.01A，放电为负，充电为正*/
    {"ambient_temp", 0x0006, &ambient_temp_s, false},                                 /* 环境温度 1°C */
    {"cell_min_temp", 0x0007, &cell_min_temp_s, false},                               /* 电芯最低温度 1°C */
    {"mos_temp", 0x0008, &mos_temp_s, false},                                         /* 板卡温度（MOS 管温度）1°C */
    {"cell_voltage_1", 0x0009, &cell_voltage_1_s, false},                             /* 电芯 1 电压 0.001V */
    {"cell_voltage_2", 0x000A, &cell_voltage_2_s, false},                             /* 电芯 2 电压 0.001V */
    {"cell_voltage_3", 0x000B, &cell_voltage_3_s, false},                             /* 电芯 3 电压 0.001V */
    {"cell_voltage_4", 0x000C, &cell_voltage_4_s, false},                             /* 电芯 4 电压 0.001V */
    {"cell_voltage_5", 0x000D, &cell_voltage_5_s, false},                             /* 电芯 5 电压 0.001V */
    {"cell_voltage_6", 0x000E, &cell_voltage_6_s, false},                             /* 电芯 6 电压 0.001V */
    {"cell_voltage_7", 0x000F, &cell_voltage_7_s, false},                             /* 电芯 7 电压 0.001V */
    {"cell_voltage_8", 0x0010, &cell_voltage_8_s, false},                             /* 电芯 8 电压 0.001V */
    {"cell_voltage_9", 0x0011, &cell_voltage_9_s, false},                             /* 电芯 9 电压 0.001V */
    {"cell_voltage_10", 0x0012, &cell_voltage_10_s, false},                           /* 电芯 10 电压 0.001V */
    {"cell_voltage_11", 0x0013, &cell_voltage_11_s, false},                           /* 电芯 11 电压 0.001V */
    {"cell_voltage_12", 0x0014, &cell_voltage_12_s, false},                           /* 电芯 12 电压 0.001V */
    {"cell_voltage_13", 0x0015, &cell_voltage_13_s, false},                           /* 电芯 13 电压 0.001V */
    {"cell_voltage_14", 0x0016, &cell_voltage_14_s, false},                           /* 电芯 14 电压 0.001V */
    {"cell_voltage_15", 0x0017, &cell_voltage_15_s, false},                           /* 电芯 15 电压 0.001V */
    {"cell_voltage_16", 0x0018, &cell_voltage_16_s, false},                           /* 电芯 16 电压 0.001V */
    {"cell_voltage_17", 0x0019, &cell_voltage_17_s, false},                           /* 电芯 17 电压 0.001V */
    {"cell_voltage_18", 0x001A, &cell_voltage_18_s, false},                           /* 电芯 18 电压 0.001V */
    {"cell_voltage_19", 0x001B, &cell_voltage_19_s, false},                           /* 电芯 19 电压 0.001V */
    {"cell_voltage_20", 0x001C, &cell_voltage_20_s, false},                           /* 电芯 20 电压 0.001V */
    {"cell_max_temp", 0x001D, &cell_max_temp_s, false},                               /* 电芯最高温度 1°C */
    {"number_of_battery_cycles", 0x001E, &number_of_battery_cycles_s, false},         /* 电池循环次数 */
    {"battery_discharge_power_max", 0x001F, &battery_discharge_power_max_s, false},   /* 电池最大放电功率 */
    {"reserve", 0x0020, &reserve_s, false},                                           /* 保留 */
    {"reserve", 0x0021, &reserve_s, false},                                           /* 保留 */
    /* 示例：2023 年 = 0x07E7
     * 高字节：0x07
     * 低字节：0xE7 */
    {"year", 0x0022, &year_s, true},                       /* 年 */
    {"month_and_day", 0x0023, &month_and_day_s, true},     /* 月，日 */
    {"hour_and_minute", 0x0024, &hour_and_minute_s, true}, /* 时，分 */
    {"second", 0x0025, &second_s, true},                   /* 秒，默认00 */

    // 电池概要信息(Battery profile)
    {"bms_hardware_version_1", 0xA001, &bms_hardware_version_1_s, false},                                   /* BMS 硬件版本1 */
    {"bms_hardware_version_2", 0xA002, &bms_hardware_version_2_s, false},                                   /* BMS 硬件版本2 */
    {"bms_hardware_version_3", 0xA003, &bms_hardware_version_3_s, false},                                   /* BMS 硬件版本3 */
    {"bms_hardware_version_4", 0xA004, &bms_hardware_version_4_s, false},                                   /* BMS 硬件版本4 */
    {"bms_hardware_version_5", 0xA005, &bms_hardware_version_5_s, false},                                   /* BMS 硬件版本5 */
    {"bms_hardware_version_6", 0xA006, &bms_hardware_version_6_s, false},                                   /* BMS 硬件版本6 */
    {"bms_hardware_version_7", 0xA007, &bms_hardware_version_7_s, false},                                   /* BMS 硬件版本7 */
    {"bms_hardware_version_8", 0xA008, &bms_hardware_version_8_s, false},                                   /* BMS 硬件版本8 */
    {"bms_hardware_version_9", 0xA009, &bms_hardware_version_9_s, false},                                   /* BMS 硬件版本9 */
    {"bms_hardware_version_10", 0xA00A, &bms_hardware_version_10_s, false},                                 /* BMS 硬件版本10 */
    {"bms_soft_version_1", 0xA00B, &bms_soft_version_1_s, false},                                           /* BMS 软件版本1 */
    {"bms_soft_version_2", 0xA00C, &bms_soft_version_2_s, false},                                           /* BMS 软件版本2 */
    {"bms_soft_version_3", 0xA00D, &bms_soft_version_3_s, false},                                           /* BMS 软件版本3 */
    {"bms_soft_version_4", 0xA00E, &bms_soft_version_4_s, false},                                           /* BMS 软件版本4 */
    {"bms_soft_version_5", 0xA00F, &bms_soft_version_5_s, false},                                           /* BMS 软件版本5 */
    {"bms_manufacturer_1", 0xA010, &bms_manufacturer_1_s, false},                                           /* BMS厂家1 */
    {"bms_manufacturer_2", 0xA011, &bms_manufacturer_2_s, false},                                           /* BMS厂家2 */
    {"bms_battery_voltage_and_capacity", 0xA012, &bms_battery_voltage_and_capacity_s, false},               /* 电池额定电压,电池额定容量 */
    {"bms_series_parallel_quantity", 0xA013, &bms_series_parallel_quantity_s, false},                       /* 电芯串联级数,并联级数 */
    {"bms_communication_protocol_soft_version", 0xA014, &bms_communication_protocol_soft_version_s, false}, /* 通信协议大版本号,通信协议小版本号 */
    {"reserve", 0xA015, &reserve_s, false},                                                                 /* 预留 */
    {"reserve", 0xA016, &reserve_s, false},                                                                 /* 预留 */
    {"reserve", 0xA017, &reserve_s, false},                                                                 /* 预留 */
    {"reserve", 0xA018, &reserve_s, false},                                                                 /* 预留 */
    {"reserve", 0xA019, &reserve_s, false},                                                                 /* 预留 */

    {"sn_code_1", 0x03E8, &sn_code_1_s, false},   /* 设备ID 1 */
    {"sn_code_2", 0x03E9, &sn_code_2_s, false},   /* 设备ID 2 */
    {"sn_code_3", 0x03EA, &sn_code_3_s, false},   /* 设备ID 3 */
    {"sn_code_4", 0x03EB, &sn_code_4_s, false},   /* 设备ID 4 */
    {"sn_code_5", 0x03EC, &sn_code_5_s, false},   /* 设备ID 5 */
    {"sn_code_6", 0x03ED, &sn_code_6_s, false},   /* 设备ID 6 */
    {"sn_code_7", 0x03EE, &sn_code_7_s, false},   /* 设备ID 7 */
    {"sn_code_8", 0x03EF, &sn_code_8_s, false},   /* 设备ID 8 */
    {"sn_code_9", 0x03F0, &sn_code_9_s, false},   /* 设备ID 9 */
    {"sn_code_10", 0x03F1, &sn_code_10_s, false}, /* 设备ID 10 */
    {"sn_code_11", 0x03F2, &sn_code_11_s, false}, /* 设备ID 11 */
    {"sn_code_12", 0x03F3, &sn_code_12_s, false}, /* 设备ID 12 */
    {"sn_code_13", 0x03F4, &sn_code_13_s, false}, /* 设备ID 13 */
    {"sn_code_14", 0x03F5, &sn_code_14_s, false}, /* 设备ID 14 */
    {"reserve_s", 0x03F6, &reserve_s, false},     /* 预留 */
    {"reserve_s", 0x03F7, &reserve_s, false},     /* 预留 */

    {"switch_B001", 0xB001, &switch_B001_s, true},  /* 开关量1 */
    {"switch_B002", 0xB002, &switch_B002_s, false}, /* 开关量2 */
    {"switch_B003", 0xB003, &switch_B003_s, false}, /* 开关量3 */
    {"switch_B004", 0xB004, &switch_B004_s, false}, /* 开关量4 */
    {"switch_B005", 0xB005, &switch_B005_s, false}, /* 开关量5 */
    {"switch_B006", 0xB006, &switch_B006_s, true},  /* 开关量6 */
    {"reserve", 0xB007, &reserve_s, false},         /* 预留 */

};

static void data_printf(uint8_t* data, uint8_t data_len) {
  for (size_t i = 0; i < data_len; i++) {
    printf("0X%x ", data[i]);
  }
  printf("\n");
}

static modbus_data_table_t modbus_dev_1_data_table = {NULL, 0,                              // 开关量表相关
                                                      register_table, register_table_len};  // 寄存器表相关

static modbus_dev_table_t modbus_dev_table[modbus_dev_table_size] = {
    {dev_bms_addr, &modbus_dev_1_data_table, sendbuffer, sendbuffer_length, data_printf, modbus_frame_max_len},
};

static void battery_profile_set(void) {
  // 硬件版本只会是32位的
  bms_hardware_version_1_s = (hardware_version[0] << 8) + (hardware_version[1] << 0);
  bms_hardware_version_2_s = (hardware_version[2] << 8) + (hardware_version[3] << 0);

  // 软件版本只会是32位的
  bms_soft_version_1_s = (soft_version[0] << 8) + (soft_version[1] << 0);
  bms_soft_version_2_s = (soft_version[2] << 8) + (soft_version[3] << 0);

  // BMS厂商
  bms_manufacturer_1_s = (bms_manufacturer[0] << 8) + (bms_manufacturer[1] << 0);
  bms_manufacturer_2_s = (bms_manufacturer[2] << 8) + (bms_manufacturer[3] << 0);

  // 额定电压，额定容量
  bms_battery_voltage_and_capacity_s = (rated_voltage << 8) + (rated_capacity << 0);

  // 电芯串并联数
  bms_series_parallel_quantity_s = (series_number_of_cells << 8) + (number_of_cells_in_parallel << 0);

  // 通讯协议版本号
  bms_communication_protocol_soft_version_s = (bms_communication_protocol_soft_version_big << 8) + (bms_communication_protocol_soft_version_small << 0);
}

static void battery_real_time_status_read_update(void) {
  // 更新电池实时状态寄存器数据，在每次接收前进行数据更新，如果后续修改tmodbus结构，则在发送前进行更新
  total_battery_pack_pressure_s  = 1;
  number_of_cells_s              = 1;
  soc_s                          = 1;
  residual_capacity_s            = 1;
  soh_s                          = 1;
  charge_and_discharge_current_s = 1;
  ambient_temp_s                 = 1;
  cell_min_temp_s                = 1;
  mos_temp_s                     = 1;
  cell_voltage_1_s               = 1;
  cell_voltage_2_s               = 1;
  cell_voltage_3_s               = 1;
  cell_voltage_4_s               = 1;
  cell_voltage_5_s               = 1;
  cell_voltage_6_s               = 1;
  cell_voltage_7_s               = 1;
  cell_voltage_8_s               = 1;
  cell_voltage_9_s               = 1;
  cell_voltage_10_s              = 1;
  cell_voltage_11_s              = 1;
  cell_voltage_12_s              = 1;
  cell_voltage_13_s              = 1;
  cell_voltage_14_s              = 1;
  cell_voltage_15_s              = 1;
  cell_voltage_16_s              = 1;
  cell_voltage_17_s              = 1;
  cell_voltage_18_s              = 1;
  cell_voltage_19_s              = 1;
  cell_voltage_20_s              = 1;
  cell_max_temp_s                = 1;
  number_of_battery_cycles_s     = 1;
  battery_discharge_power_max_s  = 1;
}

static void switch_B001_read_update(void) {
  /*
    switch_B001
    高字节
    Bit0：保留位，默认为 0
    Bit1：电芯压差大保护
    Bit2：充电电流保护
    Bit3：放电电流保护
    Bit4：短路保护
    Bit5：充电高温保护
    Bit6：放电高温保护
    Bit7：充电低温保护

    低字节
    Bit0：放电低温保护
    Bit1：充电 MOS 管损坏
    Bit2：放电 MOS 管损坏
    Bit3：模拟前端异常
    Bit4：过充电压保护 1
    Bit5：过充电压保护 2
    Bit6：过充电压保护 3
    Bit7：过充电压保护 4
     */

  typ_iow tmp;
  tmp.byte[WORD_16_H_BYTE].bit._0 = test_switch_flag;
  tmp.byte[WORD_16_H_BYTE].bit._1 = 0;
  tmp.byte[WORD_16_H_BYTE].bit._2 = 0;
  tmp.byte[WORD_16_H_BYTE].bit._3 = 0;
  tmp.byte[WORD_16_H_BYTE].bit._4 = 0;
  tmp.byte[WORD_16_H_BYTE].bit._5 = 0;
  tmp.byte[WORD_16_H_BYTE].bit._6 = 0;
  tmp.byte[WORD_16_H_BYTE].bit._7 = 0;

  tmp.byte[WORD_16_L_BYTE].bit._0 = 0;
  tmp.byte[WORD_16_L_BYTE].bit._1 = 0;
  tmp.byte[WORD_16_L_BYTE].bit._2 = 0;
  tmp.byte[WORD_16_L_BYTE].bit._3 = 0;
  tmp.byte[WORD_16_L_BYTE].bit._4 = 0;
  tmp.byte[WORD_16_L_BYTE].bit._5 = 0;
  tmp.byte[WORD_16_L_BYTE].bit._6 = 0;
  tmp.byte[WORD_16_L_BYTE].bit._7 = 0;

  switch_B001_s = tmp.word_16;
}
static void switch_B002_read_update(void) {
  /*
    switch_B002
    高字节
    Bit0：过充电压保护 5
    Bit1：过充电压保护 6
    Bit2：过充电压保护 7
    Bit3：过充电压保护 8
    Bit4：过充电压保护 9
    Bit5：过充电压保护 10
    Bit6：过充电压保护 11
    Bit7：过充电压保护 12

    低字节
    Bit0：过充电压保护 13
    Bit1：过充电压保护 14
    Bit2：过充电压保护 15
    Bit3：过充电压保护 16
    Bit4：过充电压保护 17
    Bit5：过充电压保护 18
    Bit6：过充电压保护 19
    Bit7：过充电压保护 20
     */
}
static void switch_B003_read_update(void) {
  /*
    switch_B003
    高字节
    Bit0：过放电压保护 1
    Bit1：过放电压保护 2
    Bit2：过放电压保护 3
    Bit3：过放电压保护 4
    Bit4：过放电压保护 5
    Bit5：过放电压保护 6
    Bit6：过放电压保护 7
    Bit7：过放电压保护 8

    低字节
    Bit0：过放电压保护 9
    Bit1：过放电压保护 10
    Bit2：过放电压保护 11
    Bit3：过放电压保护 12
    Bit4：过放电压保护 13
    Bit5：过放电压保护 14
    Bit6：过放电压保护 15
    Bit7：过放电压保护 16
     */
}
static void switch_B004_read_update(void) {
  /*
    switch_B004
    高字节
    Bit0：过放电压保护 17
    Bit1：过放电压保护 18
    Bit2：过放电压保护 19
    Bit3：过放电压保护 20

    低字节
    全部预留
     */
}
static void switch_B005_read_update(void) {
  /*
    switch_B005
    高字节
    Bit0：总压过压保护
    Bit1：总压欠压保护
    Bit2：超高温保护
    Bit3：环境低温告警
    Bit4：环境高温告警
    Bit5：放电限流告警
    Bit6：电芯失效

    低字节
    Bit0：单芯电压低告警（低电量告警）
    Bit1：单芯电压高告警
    Bit2：电芯低温告警
    Bit3：电芯高温告警
    Bit4：总电压高告警
    Bit5：总电压低告警
    Bit6：单芯压差大告警
    Bit7：MOS 高温告警
     */
}
static void switch_B006_read_update(void) {
  /*
    switch_B006
    高字节
    Bit0：充电 MOS 管状态 //1：MOS管闭合，0：MOS管断开
    Bit1：放电 MOS 管状态
    Bit2：预充 MOS 管状态（和放电 MOS 管并联）
    Bit3：MOS 高温保护
    Bit4：环境高温保护
    Bit5：环境低温保护

    低字节
    Bit0：BMS 工作模式 //当 bit0为 1时，BMS为正常运行模式，0为低功耗模式
     */
}

static void sn_code_read_data_update() {
  sn_code_1_s  = (sn_code[0] << 8) + (sn_code[1] << 0);
  sn_code_2_s  = (sn_code[2] << 8) + (sn_code[3] << 0);
  sn_code_3_s  = (sn_code[4] << 8) + (sn_code[5] << 0);
  sn_code_4_s  = (sn_code[6] << 8) + (sn_code[7] << 0);
  sn_code_5_s  = (sn_code[8] << 8) + (sn_code[9] << 0);
  sn_code_6_s  = (sn_code[10] << 8) + (sn_code[11] << 0);
  sn_code_7_s  = (sn_code[12] << 8) + (sn_code[13] << 0);
  sn_code_8_s  = (sn_code[14] << 8) + (sn_code[15] << 0);
  sn_code_9_s  = (sn_code[16] << 8) + (sn_code[17] << 0);
  sn_code_10_s = (sn_code[18] << 8) + (sn_code[19] << 0);
  sn_code_11_s = (sn_code[20] << 8) + (sn_code[21] << 0);
  sn_code_12_s = (sn_code[22] << 8) + (sn_code[23] << 0);
  sn_code_13_s = 0;
  sn_code_14_s = 0;
  // printf("sn_code:%s\n", sn_code);
  // printf("sn_code len:%d\n", strlen(sn_code));
  // printf("%d\n",sn_code[0]);
}

static void modbus_read_data_update() {
  battery_real_time_status_read_update();
  sn_code_read_data_update();
  switch_B001_read_update();
  switch_B002_read_update();
  switch_B003_read_update();
  switch_B004_read_update();
  switch_B005_read_update();
  switch_B006_read_update();
}

void modbus_write_data_update(void) {
  typ_iow tmp;
  tmp.word_16 = switch_B001_s;

  // printf("switch_B001_s:%d\n",switch_B001_s);
  // printf("test_switch_flag:%d\n", test_switch_flag);
  test_switch_flag = tmp.byte[WORD_16_H_BYTE].bit._0;
  // printf("test_switch_flag:%d\n", test_switch_flag);
  // tmp.byte[WORD_16_H_BYTE].bit._1 = 0;
  // tmp.byte[WORD_16_H_BYTE].bit._2 = 0;
  // tmp.byte[WORD_16_H_BYTE].bit._3 = 0;
  // tmp.byte[WORD_16_H_BYTE].bit._4 = 0;
  // tmp.byte[WORD_16_H_BYTE].bit._5 = 0;
  // tmp.byte[WORD_16_H_BYTE].bit._6 = 0;
  // tmp.byte[WORD_16_H_BYTE].bit._7 = 0;

  // tmp.byte[WORD_16_L_BYTE].bit._0 = 0;
  // tmp.byte[WORD_16_L_BYTE].bit._1 = 0;
  // tmp.byte[WORD_16_L_BYTE].bit._2 = 0;
  // tmp.byte[WORD_16_L_BYTE].bit._3 = 0;
  // tmp.byte[WORD_16_L_BYTE].bit._4 = 0;
  // tmp.byte[WORD_16_L_BYTE].bit._5 = 0;
  // tmp.byte[WORD_16_L_BYTE].bit._6 = 0;
  // tmp.byte[WORD_16_L_BYTE].bit._7 = 0;
}

void tieta_zk_v1_3_init(void) {
  battery_profile_set();
  modbus_dev_reg(modbus_dev_table, modbus_dev_table_size);
}
void tieta_zk_v1_3_receive(uint8_t* data, uint16_t data_len) {
  if (data_len <= 0) {
    return;
  }
  if (data[1] == 0X0A) modbus_read_data_update();  // 如果是读取指令，则处理前先更新数据
  modbus_receive(data, data_len);
  if (data[1] == 0X0B) modbus_write_data_update();  // 如果是写指令，则处理后更新数据
}

void tieta_zk_v1_3_send(uint8_t* data, uint16_t data_len) {
  uint16_t r_send_len;
  r_send_len = mobuds_send(data, data_len);
  data_printf(data, r_send_len);
}